/**
 * (C) Copyright 2012 ZWW
 */
package common;

import java.util.ArrayList;

/**
 * 全排列
 * @author ZWW
 * @date 2014-3-22 下午3:18:39
 * @version V1.0
 */
public class Arrange {
	private int total = 0;
	private ArrayList arrangeList = new ArrayList();

	public Arrange() {
	}

	private void swap(String list[], int k, int i) {
		String c3 = list[k];
		list[k] = list[i];
		list[i] = c3;
	}

	public void perm(String list[], int k, int m) {
		if (k > m) {
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i <= m; i++) {
				sb.append(list[i]).append(",");
			}
			if (sb.length()>0) {
				sb.setLength(sb.length()-1);
			}
			arrangeList.add(sb.toString());
			total++;
		} else {
			for (int i = k; i <= m; i++) {
				swap(list, k, i);
				perm(list, k + 1, m);
				swap(list, k, i);
			}
		}
	}

	public int getTotal() {
		return total;
	}
	
	public ArrayList getArrangeList() {
		return arrangeList;
	}

	public static void main(String args[]) {
		String list[] = { "1", "2", "3", "4", "5" };
		Arrange ts = new Arrange();
		//ts.perm(list, 3, list.length-1);
		ts.perm(list, 5, 1);
		for (int i = 0; i < ts.getArrangeList().size(); i++) {
			System.out.println(ts.getArrangeList().get(i));
		}
		System.out.println("total:" + ts.total);
	}

}
